package com.gurun.codes.business.common;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.gurun.codes.App;
import com.gurun.codes.R;
import com.gurun.codes.business.login.LoginActivity;

/**
 * Created by harry on 2018/6/12.
 */
public class CommonTipsDialogActivity extends AppCompatActivity {
    private static final String ARG_CONTENT = "content";
    private static final String ARG_ACTION = "action";
    public static final int ACTION_LOGOUT = 1;

    public static void startActivity(Context context, String content, int action) {
        Intent intent = new Intent(context, CommonTipsDialogActivity.class);
        intent.putExtra(ARG_CONTENT, content);
        intent.putExtra(ARG_ACTION, action);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String content = getIntent().getStringExtra(ARG_CONTENT);
        final int action = getIntent().getIntExtra(ARG_ACTION, -1);
        if (TextUtils.isEmpty(content)) {
            content = "";
        }

        if (action == ACTION_LOGOUT) {
            App.getInstance().clearData();
        }

        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content(content)
                .negativeText(getResources().getString(R.string.common_know))
                .canceledOnTouchOutside(false)
                .onNegative(null)
                .dismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                        if (action == ACTION_LOGOUT) {
                            App.getInstance().removeAllActivity();
                            LoginActivity.startActivity(CommonTipsDialogActivity.this);
                        }
                        finish();
                    }
                })
                .show();

        dialog.getActionButton(DialogAction.NEGATIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (action == ACTION_LOGOUT) {
                    App.getInstance().removeAllActivity();
                    LoginActivity.startActivity(CommonTipsDialogActivity.this);
                }
                finish();
            }
        });
    }
}
